﻿using System.Linq;
using System.Net;
using System.Web.Mvc;
using System.Web.Routing;
using ResourcesOverMvc.Web.Mvc;
using Samples.WebService.Models;

namespace Samples.WebService.Controllers
{
    public class ProductsController : Controller, IAllowedVerbs
    {
        public string GetAllowedVerbs(ActionExecutedContext actionExecutedContext)
        {
            switch (actionExecutedContext.ActionDescriptor.ActionName.ToUpper())
            {
                case "DETAILS":
                    return "GET, PUT";

                default:
                    return "GET, POST";
            }
        }

        [HttpGet]
        public ActionResult Index()
        {
            var products = Product.AllProducts;

            foreach (var product in products)
            {
                product.Uri = Url.Action("Details", "Products", new RouteValueDictionary { { "Id", product.Id } }, Request.Url.Scheme, string.Empty);
            }

            return View(Product.AllProducts);
        }

        [HttpPost]
        public ActionResult Index(Product newProduct)
        {
            // Validate the category exists
            if (!Category.AllCategories.Exists(c => c.CategoryId == newProduct.CategoryId))
            {
                return this.View("ErrorDetail", new ErrorDetail("The specified category does not exist."), HttpStatusCode.BadRequest);
            }

            newProduct.Id = Product.AllProducts.Max(p => p.Id) + 1;
            newProduct.Uri = Url.Action("Details", "Products", new RouteValueDictionary { { "Id", newProduct.Id } }, Request.Url.Scheme, string.Empty);
            Product.AllProducts.Add(newProduct);
            return View("Details", newProduct);
        }

        [HttpGet]
        public ActionResult Details(int id)
        {
            var product = Product.AllProducts.First(p => p.Id == id);

            if (product == null)
            {
                return this.View("ErrorDetail", new ErrorDetail("The specified product does not exist."), HttpStatusCode.NotFound);
            }

            product.Uri = Url.Action("Details", "Products", new RouteValueDictionary { { "Id", product.Id } }, Request.Url.Scheme, string.Empty);

            return View(product);
        }

        [HttpPut]
        public ActionResult Details(int id, Product newVersionOfProduct)
        {
            // Validate the category exists
            if (!Category.AllCategories.Exists(c => c.CategoryId == newVersionOfProduct.CategoryId))
            {
                return this.View("ErrorDetail", new ErrorDetail("The specified category does not exist."), HttpStatusCode.BadRequest);
            }

            Product.AllProducts.Remove(id);
            newVersionOfProduct.Id = id;
            Product.AllProducts.Add(newVersionOfProduct);
            newVersionOfProduct.Uri = Url.Action("Details", "Products", new RouteValueDictionary { { "Id", newVersionOfProduct.Id } }, Request.Url.Scheme, string.Empty);
            return View("Details", newVersionOfProduct);
        }
    }
}
